<template>
  <div class="login-container">
    <!-- 使用v-once确保樱花飘落只渲染一次 -->
    <div class="cherry-blossom-container" v-once>
      <div v-for="n in 100" :key="n" class="petal" :style="getRandomPosition(n)"></div>
    </div>

    <!-- 右侧登录框 -->
    <div class="right-side">
      <div class="login-box">
        <h2>登录</h2>

        <!-- 登录方式切换Tab -->
        <div class="login-tabs">
          <div
            v-for="(tab, index) in tabs"
            :key="tab.id"
            class="tab-item"
            :class="{ active: activeTabIndex === index }"
            @click="switchTab(index)"
          >
            {{ tab.name }}
          </div>
          <!-- 滑动指示器 -->
          <div class="tab-slider" :style="sliderStyle"></div>
        </div>

        <!-- Tab内容区域 -->
        <div class="tab-content">
          <div class="tab-content-inner" :style="contentStyle">
            <div class="tab-pane">
              <AccountLogin @login="handleLoginEvent" />
            </div>
            <div class="tab-pane">
              <CodeLogin @login="handleLoginEvent" />
            </div>
            <div class="tab-pane">
              <QrCodeLogin @login="handleLoginEvent" />
            </div>
          </div>
        </div>

        <div class="register-link">还没有账号？<a href="#">立即注册</a></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import AccountLogin from './components/AccountLogin.vue'
import CodeLogin from './components/CodeLogin.vue'
import QrCodeLogin from './components/QrCodeLogin.vue'

// 定义标签页
const tabs = [
  { id: 'account', name: '账号密码' },
  { id: 'code', name: '验证码登录' },
  { id: 'qrcode', name: '扫码登录' },
]

// 当前激活的标签页索引
const activeTabIndex = ref(0)

// 预计算所有樱花花瓣的位置和动画属性，避免重新计算
const petalPositions = Array.from({ length: 100 }).map(() => {
  const size = Math.random() * 10 + 10
  const startLeft = Math.random() * 100
  const animationDelay = Math.random() * 10
  const animationDuration = Math.random() * 5 + 10

  return {
    left: `${startLeft}vw`,
    width: `${size}px`,
    height: `${size}px`,
    animationDelay: `${animationDelay}s`,
    animationDuration: `${animationDuration}s`,
  }
})

// 获取预计算的樱花花瓣位置
function getRandomPosition(index: number) {
  return petalPositions[index % petalPositions.length]
}

// 计算滑块样式
const sliderStyle = computed(() => {
  const width = 100 / tabs.length
  return {
    width: `${width}%`,
    left: `${activeTabIndex.value * width}%`,
  }
})

// 计算内容区样式
const contentStyle = computed(() => {
  return {
    transform: `translateX(-${activeTabIndex.value * 33}%)`,
  }
})

// 切换标签页
const switchTab = (index: number) => {
  activeTabIndex.value = index
}

// 处理登录事件
interface AccountLoginData {
  type: 'account'
  username: string
  password: string
  remember: boolean
}

interface CodeLoginData {
  type: 'code'
  phone: string
  verifyCode: string
}

interface QrCodeLoginData {
  type: 'qrcode'
  qrcodeId: string
}

type LoginData = AccountLoginData | CodeLoginData | QrCodeLoginData

const handleLoginEvent = (data: LoginData) => {
  console.log('登录事件：', data)

  switch (data.type) {
    case 'account':
      console.log('账号密码登录：', data)
      // 这里处理账号密码登录逻辑
      break
    case 'code':
      console.log('验证码登录：', data)
      // 这里处理验证码登录逻辑
      break
    case 'qrcode':
      console.log('二维码登录：', data)
      // 这里处理二维码登录逻辑
      break
  }

  // 登录成功后跳转
  // router.push('/')
}
</script>

<style scoped>
.login-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  background-image: url('../../assets/image/wallhaven-j3xxw5.png');
  background-size: cover;
  background-position: center;
  overflow: hidden;
}

.cherry-blossom-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 1;
}

/* 左侧区域 */
.left-side {
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2;
}

/* 右侧区域 */
.right-side {
  position: absolute;
  top: 0;
  right: 0;
  width: 50%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2;
}

.petal {
  position: absolute;
  top: -10%;
  background-color: #ffd7e5;
  border-radius: 150% 0 150% 0;
  animation: falling linear infinite both;
  filter: drop-shadow(0 0 10px rgba(255, 215, 229, 0.8));
  z-index: 1;
}

.petal:nth-child(even) {
  background-color: #ffb7d5;
  border-radius: 0 150% 0 150%;
}

.petal:nth-child(3n) {
  background-color: #ffcce5;
  border-radius: 150% 150% 0 0;
}

@keyframes falling {
  0% {
    top: -10%;
    transform: rotate(0deg) translateX(0);
    opacity: 1;
  }
  50% {
    transform: rotate(360deg) translateX(100px);
    opacity: 0.7;
  }
  100% {
    top: 100%;
    transform: rotate(720deg) translateX(-100px);
    opacity: 0;
  }
}

/* 登录框 */
.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(3px);
}

h2 {
  text-align: center;
  /* 渐变文字 */
  background: linear-gradient(45deg, #ff69b4, #8dd9ff);
  background-clip: text;
  color: transparent;
  margin-bottom: 30px;
  font-size: 28px;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.register-link a {
  /* 渐变文字 */
  background: linear-gradient(45deg, #ff69b4, #8dd9ff);
  background-clip: text;
  color: transparent;
  font-weight: bold;
}

/* 登录方式切换Tab */
.login-tabs {
  display: flex;
  margin-bottom: 25px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 10px 0;
  cursor: pointer;
  color: #666;
  font-size: 14px;
  transition: all 0.3s;
  position: relative;
  z-index: 2;
}

.tab-item.active {
  color: #ff69b4;
  font-weight: bold;
}

/* 滑动指示器 */
.tab-slider {
  position: absolute;
  bottom: 0;
  height: 2px;
  background: linear-gradient(45deg, #ff69b4, #8dd9ff);
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 4px;
}

/* Tab内容区域 */
.tab-content {
  min-height: 280px;
  overflow: hidden;
}

.tab-content-inner {
  display: flex;
  width: 300%;
  transition: transform 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.tab-pane {
  flex: 1;
  padding: 0 10px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .left-side,
  .right-side {
    position: relative;
    width: 100%;
    height: auto;
  }

  .login-container {
    flex-direction: column;
    padding: 40px 0;
  }

  .login-box {
    margin-top: 30px;
    width: 90%;
    max-width: 400px;
  }
}
</style>

<script lang="ts">
export default {
  name: 'LoginView',
}
</script>
